﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization;

namespace AMSService.model
{
    [DataContract (IsReference=true)]//(Namespace="AMSService.model")]
    public class AMSImage : DBConnector
    {
        private int _id = 0;
        private Image _img;
        private string _capt;
        private Asset _asset;
        private PurchaseData _purch;
        private ScheduleData _sched;
        private ServiceData _serv;
        private WarrantyData _warr;

        // Stored procedure names
        private const string _spgetimagebyid = "GetImageById";
        private const string _spdeleteimagebyid = "DeleteImageById";
        private const string _spupdateimagebyid = "UpdateImage";
        private const string _spinsertimage = "InsertImage";

        // Stored procedure arguments
        private const string _prmid = "@id";
        private const string _prmarg = "@arg";
        private const string _prmimg = "@img";
        private const string _prmcapt = "@capt";
        private const string _prmassetid = "@assetid";
        private const string _prmpurchid = "@purchid";
        private const string _prmschedid = "@schedid";
        private const string _prmservid = "@servid";
        private const string _prmwarrid = "@warrid";

        // Table field names
        private const string _fldid = "pkid";
        private const string _fldimg = "img";
        private const string _fldcapt = "caption";
        private const string _fldassetid = "fkassetid";
        private const string _fldpurchid = "fkpurchid";
        private const string _fldschedid = "fkschedid";
        private const string _fldservid = "fkservid";
        private const string _fldwarrid = "fkwarrid";

        // Stored procedure argument enumeration
        public enum _spargs { pk, assetid, purchid, schedid, servid, warrid };

        public AMSImage(Asset BaseItem)
        {
            _asset = BaseItem;
            _id = 0;
        }

        public AMSImage(Asset BaseItem, int ID)
        {
            _asset = BaseItem;
            FillImageData(ID);
        }

        public AMSImage(Asset BaseItem, DataRow Row)
        {
            _asset = BaseItem;
            FillImageData(Row);
        }

        public AMSImage(Asset BaseItem, PurchaseData Purchase)
        {
            _asset = BaseItem;
            _purch = Purchase;
            _id = 0;
        }

        public AMSImage(Asset BaseItem, PurchaseData Purchase, int ID)
        {
            _asset = BaseItem;
            _purch = Purchase;
            FillImageData(ID);
        }

        public AMSImage(Asset BaseItem, PurchaseData Purchase, DataRow Row)
        {
            _asset = BaseItem;
            _purch = Purchase;
            FillImageData(Row);
        }

        public AMSImage(Asset BaseItem, ScheduleData Schedule)
        {
            _asset = BaseItem;
            _sched = Schedule;
            _id = 0;
        }

        public AMSImage(Asset BaseItem, ScheduleData Schedule, int ID)
        {
            _asset = BaseItem;
            _sched = Schedule;
            FillImageData(ID);
        }

        public AMSImage(Asset BaseItem, ScheduleData Schedule, DataRow Row)
        {
            _asset = BaseItem;
            _sched = Schedule;
            FillImageData(Row);
        }

        public AMSImage(Asset BaseItem, ServiceData Service)
        {
            _asset = BaseItem;
            _serv = Service;
            _id = 0;
        }

        public AMSImage(Asset BaseItem, ServiceData Service, int ID)
        {
            _asset = BaseItem;
            _serv = Service;
            FillImageData(ID);
        }

        public AMSImage(Asset BaseItem, ServiceData Service, DataRow Row)
        {
            _asset = BaseItem;
            _serv = Service;
            FillImageData(Row);
        }

        public AMSImage(Asset BaseItem, WarrantyData Warranty)
        {
            _asset = BaseItem;
            _warr = Warranty;
            _id = 0;
        }

        public AMSImage(Asset BaseItem, WarrantyData Warranty, int ID)
        {
            _asset = BaseItem;
            _warr = Warranty;
            FillImageData(ID);
        }

        public AMSImage(Asset BaseItem, WarrantyData Warranty, DataRow Row)
        {
            _asset = BaseItem;
            _warr = Warranty;
            FillImageData(Row);
        }

        public static string SpSelect
        {
            get { return _spgetimagebyid; }
        }

        public static string SpPrmId
        {
            get { return _prmid; }
        }

        public static string SpPrmArg
        {
            get { return _prmarg; }
        }

        [DataMember]
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        [DataMember]
        public Image Image
        {
            get { return _img; }
            set { if (value != null) { _img = value; } }
        }

        [DataMember]
        public string Caption
        {
            get { return _capt; }
            set { _capt = value; }
        }

        // Deletes this image record from the database.
        public bool Delete()
        {
            List<SqlParameter> paramlist = new List<SqlParameter>();
            SqlParameter pid = new SqlParameter(_prmid, _id);
            paramlist.Add(pid);
            
            return SendWriteRequest(_spdeleteimagebyid, paramlist) > 0;
        }

        // Loads this image's data from the specified data row.
        private void FillImageData(DataRow dr)
        {
            _img = ParseImage(dr);
            _capt = dr[_fldcapt].ToString();
        }

        // Loads this image's data from the database by the specified primary key.
        private void FillImageData(int ID)
        {
            List<SqlParameter> paramlist = new List<SqlParameter>();
            SqlParameter pid = new SqlParameter(_prmid, _id);
            SqlParameter parg = new SqlParameter(_prmarg, (int)_spargs.pk);
            paramlist.Add(pid);
            paramlist.Add(parg);
            DataTable dt = SendReadRequest(_spgetimagebyid, paramlist);

            if (dt.Rows.Count > 0)
            {
                FillImageData(dt.Rows[0]);
            }
        }

        // Creates an image object from the image field of the specified data row.
        private Image ParseImage(DataRow dr)
        {
            byte[] barrImg = (byte[])dr[_fldimg];
            MemoryStream mstream = new MemoryStream(barrImg);
            return Image.FromStream(mstream);
        }

        // Saves this image to the database.  If this object does not have a
        // primary key value, this method creates a new record.  If it does have
        // a primary key value, this method updates the existing record.
        public bool Save()
        {
            List<SqlParameter> paramlist = new List<SqlParameter>();
            SqlParameter pid = new SqlParameter(_prmid, _id);
            SqlParameter pimg = new SqlParameter(_prmimg, _img);
            SqlParameter pcapt = new SqlParameter(_prmcapt, _capt);

            paramlist.Add(pimg);
            paramlist.Add(pcapt);

            // If this is an update request (the id already exists)
            if (_id > 0)
            {
                paramlist.Add(pid);
                return SendWriteRequest(_spupdateimagebyid, paramlist) > 0;
            }
            else
            {
                _id = SendWriteRequest(_spinsertimage, paramlist);
                return _id > 0;
            }
        }
    }
}